package com.workplan.client.widgets.currentemployees;

import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.widget.WidgetPresenter;

import com.general.client.action.ActionHandler;
import com.general.client.widgets.yesnomodal.YesNoModal;
import com.general.client.widgets.yesnomodal.YesNoModalPresenter;
import com.general.client.widgets.yesnomodal.YesNoModalPresenterImpl;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.workplan.client.widgets.employeeitem.EmployeeItem;
import com.workplan.client.widgets.employeeitem.EmployeeItemPresenter;
import com.workplan.client.widgets.employeeitem.EmployeeItemPresenterImpl;
import com.workplan.client.widgets.historymodal.HistoryModal;
import com.workplan.client.widgets.historymodal.HistoryModalPresenter;
import com.workplan.client.widgets.historymodal.HistoryModalPresenterImpl;
import com.workplan.shared.datamodel.employee.EmployeeDataModel;
import com.workplan.shared.datamodel.employee.EmployeeDataModel.Role;

public class CurrentEmployeesPresenterImpl extends
		WidgetPresenter<CurrentEmployeesPresenter.Display> implements
		CurrentEmployeesPresenter {

	private final HistoryModalPresenter.Display historyModal = new HistoryModal();
	private final HistoryModalPresenter historyModalPresenter;

	private final YesNoModalPresenter.Display yesNoModal = new YesNoModal();
	private final YesNoModalPresenter yesNoModalPresenter;

	public CurrentEmployeesPresenterImpl(Display display, EventBus eventBus) {
		super(display, eventBus);
		historyModalPresenter = new HistoryModalPresenterImpl(historyModal,
				eventBus);
		yesNoModalPresenter = new YesNoModalPresenterImpl(yesNoModal, eventBus);
	}

	@Override
	protected void onBind() {
		yesNoModalPresenter.bind();
		yesNoModalPresenter
				.setText("Are you sure you want to remove this employee?");
		display.setYesNoModal(yesNoModalPresenter.getDisplay());
		historyModalPresenter.bind();
		display.setHistoryModal(historyModalPresenter.getDisplay());
		historyModalPresenter.show(false);
		display.init();

		// TODO bring data from a service

		// TODO remove this dummy data
		for (int i = 0; i < 8; i++) {
			EmployeeItemPresenter eip = createEmployeeItem(eventBus);
			registerHandler(eip.addHistoryActionHandler(new ActionHandler() {

				@Override
				public <H extends EventHandler> void onAction(GwtEvent<H> event) {
					historyModalPresenter.show(true);
				}
			}));
			registerHandler(eip.addRemoveActionHandler(new ActionHandler() {

				@Override
				public <H extends EventHandler> void onAction(GwtEvent<H> event) {
					yesNoModalPresenter.show(true);
				}
			}));

			display.addListItem(eip.getDisplay());
		}

	}

	// TODO remove this dummy data
	private EmployeeItemPresenter createEmployeeItem(EventBus eventBus) {
		EmployeeItemPresenter.Display employeeItem = new EmployeeItem();
		EmployeeItemPresenter employeeItemPresenter = new EmployeeItemPresenterImpl(
				employeeItem, eventBus);
		employeeItemPresenter.bind();
		EmployeeDataModel employeeDataModel = new EmployeeDataModel(1, "Nadav",
				"Greenberg", "0503335855", "grnadav@gmail.com", 2, 4,
				Role.SHIFT_MANAGER);
		employeeItemPresenter.setModel(employeeDataModel);
		return employeeItemPresenter;
	}

	@Override
	protected void onUnbind() {
		historyModalPresenter.unbind();
		yesNoModalPresenter.unbind();
	}

	@Override
	protected void onRevealDisplay() {
	}

}
